package com.usc.mock.db.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usc.mock.db.bean.ActivityInfo;
import com.usc.mock.db.mapper.ActivityInfoMapper;
import com.usc.mock.db.service.ActivityInfoService;
import com.usc.mock.utlil.RandomNum;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 活动表 服务实现类
 * </p>
 */
@Service
public class ActivityInfoServiceImpl extends ServiceImpl<ActivityInfoMapper, ActivityInfo> implements ActivityInfoService {


    @Value("${mock.date}")
    String mockDate;

    public void updateRecentlyDate() {
        List<ActivityInfo> activityInfoList = list();

        for (ActivityInfo activityInfo : activityInfoList) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = null;
            Date stDate = null;
            Date edDate = null;
            try {
                date = simpleDateFormat.parse(mockDate);
                stDate = DateUtils.addDays(date, 0 - (RandomNum.getRandInt(1, 20)));
                edDate = DateUtils.addDays(date, (RandomNum.getRandInt(1, 20)));
            } catch (ParseException e) {
                e.printStackTrace();
            }


            activityInfo.setStartTime(stDate);
            activityInfo.setEndTime(edDate);
        }
        saveOrUpdateBatch(activityInfoList);
    }
}
